<?php

//针对用户表的模型
namespace admin\model;
use \core\Model;

class UserModel extends Model{
    protected $table='user';
    //通过用户名获取用户信息
    public function getUserByUsername($username){
        //防止sql注入
        $username=addslashes($username);


        $sql="select * from {$this->getTable()} where u_username='{$username}'";

        //执行sql
      return  $this->query($sql);
    }
    //获取用户数量
    public function getCounts(){
        //组织sql语句
        $sql="select count(*) as c from {$this->getTable()}";

        //获取结果 保留数据
        $res=$this->query($sql);

//        返回结果
        return $res['c'] ?? 0;
    }
    //验证用户是否存在
    public function checkUsername($username){
        //组织sql并且执行他
        $sql="select id from {$this->getTable()} where u_username='{$username}'";

        return $this->query($sql);
    }
    //按照用户分页获取用户分页信息
    public function getAllUser($pagecount = 5,$page=1){
        //计算页码
        $offset = ($page - 1) * $pagecount;
        //组织sql获取用户信息
        $sql="select id,u_username,u_is_admin,u_reg_time from {$this->getTable()} order by u_reg_time desc limit {$offset},{$pagecount}";
        //执行sql
        return $this->query($sql,true);
    }
}
